package com.uptown.matrix.web.controller;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.uptown.matrix.common.result.WebResult;
import com.uptown.matrix.core.producer.instance.KafkaProducerService;
import com.uptown.matrix.core.producer.ProducerService;
import com.uptown.matrix.core.producer.instance.RedisProducerService;
import com.uptown.matrix.core.producer.instance.RocketMqProducerService;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 发送消息
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    RedisProducerService redisProducerService;

    @Resource
    KafkaProducerService kafkaProducerService;

    @Resource
    RocketMqProducerService rocketMqProducerService;

    private Map<String, ProducerService> specServiceMap = new HashMap<>();

    @PostConstruct
    void init() {
        specServiceMap.put("kafka", kafkaProducerService);
        specServiceMap.put("redis", redisProducerService);
        specServiceMap.put("rocketmq", rocketMqProducerService);
    }

    @SneakyThrows
    @RequestMapping("/product")
    public WebResult productSend(@RequestParam("component_type") String componentType,
                                 @RequestParam("boot_server") String bootServer,
                                 String group,
                                 String topic,
                                 String message) {

        if (!specServiceMap.containsKey(componentType)) {
            throw new Exception("暂不支持消息组件类型");
        }
        return specServiceMap.get(componentType).producerSend(bootServer, group, topic, message);
    }

}
